package com.dhecp.project.hr.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.hr.domain.AmazonAddress;
import com.dhecp.project.hr.mapper.AmazonAddressMapper;
import com.dhecp.project.hr.service.AmazonAddressService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @program: kjecp
 * @description: 亚马逊地址设置
 * @author: fxz
 * @create: 2022-10-09 14:36
 */
@Service
public class AmazonAddressServiceImpl extends BaseModuleServiceImpl implements AmazonAddressService {

    @Autowired
    private AmazonAddressMapper businessMapper;

    @Autowired
    private UpdateLogService updateLogService;
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * 查询
     * @param params
     * @return List - AttendanceQJ
     * @throws Exception
     * @author DH-Xxj
     */
    @Override
    public List<AmazonAddress> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "guid asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }
        return businessMapper.select(params);
    }

    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.AmazonAddress
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public AmazonAddress findByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return businessMapper.findById(params);
    }

    /**
     * 新增亚马逊地址设置
     * @author Jason
     * @date: 2022-09-02 21:40
     */
    @Override
    public AjaxResult insert(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        AmazonAddress entity = JSON.parseObject(JSON.toJSONString(params), AmazonAddress.class);
        entity.setGuid(RandomValueUtils.getGUID());
        if (entity.getGuid() == null || "".equals(entity.getGuid())){
            entity.setGuid(RandomValueUtils.getGUID());
        }

        if (entity.getAddressLine1() == null || entity.getAddressLine1().length() == 0) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
//        if (entity.getAddressLine2() == null || entity.getAddressLine2().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getCountryCode() == null || entity.getCountryCode().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getCity() == null || entity.getCity().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getDistrictOrCounty() == null || entity.getDistrictOrCounty().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getStateOrProvinceCode() == null || entity.getStateOrProvinceCode().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getPostalCode() == null || entity.getPostalCode().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }

        businessMapper.insert(entity);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
    }

    /**
     * 修改亚马逊地址设置
     * @author fxz
     * @date: 2022-09-02 21:40
     */
    @Override
    public AjaxResult update(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        AmazonAddress entity = JSON.parseObject(JSON.toJSONString(params), AmazonAddress.class);

        if (entity.getGuid() == null || "".equals(entity.getGuid())){
            entity.setGuid(RandomValueUtils.getGUID());
        }
//
//        if (entity.getAddressLine1() == null || entity.getAddressLine1().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getAddressLine2() == null || entity.getAddressLine2().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getCountryCode() == null || entity.getCountryCode().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getCity() == null || entity.getCity().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getDistrictOrCounty() == null || entity.getDistrictOrCounty().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getStateOrProvinceCode() == null || entity.getStateOrProvinceCode().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }
//        if (entity.getPostalCode() == null || entity.getPostalCode().length() == 0) {
//            return AjaxResult.error("数据提交失败，必填参数有误！");
//        }

        businessMapper.update(entity);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
    }



    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @Author: Jason
     * @date: 2022-09-02 21:40
     */
    @Override
    public AmazonAddress queryFirstByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return businessMapper.queryFirstByGuid(params);
    }

    /**
     * 删除
     * @param params
     * @return AjaxResult
     * @author DH-Xxj
     */
    @Override
    public AjaxResult deleteData(Map<String, Object> params) {
        List<AmazonAddress> businessDataList = JSON.parseArray(params.get("infoItems").toString(), AmazonAddress.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (AmazonAddress businessData : businessDataList) {
            businessMapper.delete(businessData);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}